//
//  UINavigationController+MY.m
//  JQLive
//
//  Created by Bruce on 2020/3/7.
//  Copyright © 2020 mlqk. All rights reserved.
//

#import "UINavigationController+MY.h"

static char UINavigationControllerMarkViewControllerKey;

@implementation UINavigationController (MY)

- (void)setMarkViewController:(UIViewController *)markViewController
{
    if(markViewController && [self.viewControllers containsObject:markViewController])
    {
        NSInteger index = [self.viewControllers indexOfObject:markViewController];
        objc_setAssociatedObject(self, &UINavigationControllerMarkViewControllerKey, @(index), OBJC_ASSOCIATION_ASSIGN);
    }
}

- (UIViewController *)markViewController
{
    NSInteger index = [objc_getAssociatedObject(self, &UINavigationControllerMarkViewControllerKey) integerValue];
    return [self.viewControllers objectAtIndex:index];
}

- (void)my_popToMarkViewController
{
    if(self.markViewController)
    {
        [self popToViewController:self.markViewController animated:YES];
        objc_removeAssociatedObjects(objc_getAssociatedObject(self, &UINavigationControllerMarkViewControllerKey));
    }
}

@end
